package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: tantan
 * @date: 2020/4/19-15:04
 * @Version: 1.0.0
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public List<Permission> findAll() {
        return permissionMapper.findAll();

    }
    @Override
    public void add(CheckItem checkItem) {

        permissionMapper.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
//        ​	2、现在我们只要使用pagehelper，实现分页，注意：28行代码和30行顺序不能换
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());//原理就是mybatis拦截器
//​			3、此时我们需要写一个普普通通的查询sql和分页毫无关系
        List<Permission> Permission = permissionMapper.findPage(queryPageBean.getQueryString());

//        Page<CheckItem> page =  checkItemMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), Permission);
    }

    @Override
    public void delete(Integer id) {
//        3、我们删除之前需要判断一下当前的检查项有没有被别人引用，，
        /* Integer count = checkItemMapper.fincCountByCheckItemId(id);*/
      /*  if(count > 0){
//            引用了就需要给用户提示不能删除
            throw new CheckItemException("存在引用不能删除");
        } else {
            //没有直接删除*/
        permissionMapper.delete(id);
        /*  }*/

    }

    @Override
    public Permission findById(Integer id) {
        return permissionMapper.findById(id);
    }

    @Override
    public void edit(Permission Permission) {
        permissionMapper.edit(Permission);
    }
}
